#-*- coding=utf-8 -*-
'''
@author:zhangchao
@time 2018/04/23
'''

import os
import sys
import time
import source.util.GetDevice as GetDevice
from source.util.LogUtil import Log,log
import threading

import source.core.Controller as Controller


# currentV_view=os.path.split(os.path.)
# print(os.path.dirname(__file__))
PATH = os.path.abspath(os.path.dirname(__file__))
sys.path.append(PATH)

class MyThread(threading.Thread):
    def __init__(self,device,file_list,num):
        '''
        :param device: 执行设备信息
        :param file_list: -f -d 传入文件信息
        :param num: 循环次数
        '''
        super(MyThread, self).__init__()  # 注意：一定要显式的调用父类的初始化函数。
        self.device = device
        self.file_list=file_list
        self.num=num
        pass

    @log()
    def run(self):
        #FIXME package名称需要从配置文件中读取，不能写死
        run_case= Controller.RunApp(self.device,self.file_list)
        for i in range(self.num):
            print "current times=" + str(i + 1)
            run_case.case_start()
            print "current times=" + str(i + 1)+"end"
        Log().info("任务执行结束")
        return

def device_run(device,file_list):
    run_case = Controller.RunApp(device,file_list)
    run_case.case_start()

@log()
def run_device(file_list,num):
    GetDevice.set_device_yaml()
    device_list= Controller.get_device_info()
    # device_run(device_list[0],file_list)
    for device in device_list:
       t = MyThread(device,file_list,num)
       # t.setDaemon(True)
       t.start()
       t.join()
